<template>
  <div>
    <div class="header">
      <div class="same">
        <div class="til">
          <i class="el-icon-arrow-left" @click="goto"></i>
          <span>修改地址</span>
        </div>
        <div class="search">
          <span class="city">
            {{ city == undefined ? "沈阳市" : city }}

            <i class="el-icon-caret-bottom"></i>
          </span>

          <i class="el-icon-search"></i>
          <input
            type="text"
            v-model="search_val"
            placeholder="请输入您的地址"
          />
        </div>
      </div>
    </div>
    <div class="main" v-show="search_val === ''">
      <div class="same">
        <div class="address">
          <i class="el-icon-location-information"></i>
          <span>{{ district == undefined ? "沈阳市浑南区" : district }}</span>
          <i class="el-icon-aim"></i>
          <p @click="$router.go(0)">重新定位</p>
        </div>
      </div>
      <div class="address-list">
        <div class="addressa" v-for="item in list" :key="item.addressId">
          <div class="addre" @click="selectAddress(item, 1)">
            <div class="til">
              <p>
                {{ item.name }}<span>{{ item.telephone }}</span>
                <van-tag type="danger">{{
                  item.isDefault === true ? "默认" : ""
                }}</van-tag>
              </p>
            </div>
            <div class="detailedAddres">{{ item.detailedAddress }}</div>
          </div>
          <div class="edit">
            <router-link :to="'/City/' + item.addressId"
              ><i class="el-icon-edit"></i
            ></router-link>
          </div>
        </div>
      </div>
      <van-button type="danger" block @click="$router.push('/City/0')"
        >新增地址</van-button
      >
    </div>
    <div class="main" v-show="search_val != ''">
      <div
        class="tips"
        v-for="(item, i) in areaListAdd"
        :key="'areaList' + i"
        @click="selectAddress(item, 2)"
      >
        <div class="same">
          <dl>
            <dt>{{ item.name }}</dt>
            <dd>{{ item.district + item.address }}</dd>
          </dl>
          <span></span>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { AddressList } from "@/api/address";
export default {
  components: {},
  watch: {
    search_val() {
      this.searchPlace();
    },
    // list: {
    //   handler() {
    //     localStorage.setItem("list", JSON.stringify(this.list));
    //   },
    //   deep: true,
    // },
  },

  data() {
    return {
      checked: 0,
      city: "", //当前城市
      search_val: "",
      address: "",
      district: "",
      areaListAdd: [],
      chosenAddressId: "",
      list: [],
    };
  },
  beforeRouteEnter(to, from, next) {
    console.log(to);

    next((vm) => {
      vm.city = to.params.city;
      vm.address = to.params.address;
      vm.district = to.params.district;
    });
    // this.$store.types.dispatch("SET_CITY", to.params.city);
    // this.$store.types.dispatch("SET_DISTRICT", to.params.district);
  },
  created() {
    this.AddressList();
  },

  methods: {
    goto() {
      this.$router.go(-1);
    },
    AddressList() {
      let userId = this.$store.state.user.id;

      AddressList(userId).then((res) => {
        // console.log(res, "address");
        this.list = res.data.data;
      });
    },

    searchPlace() {
      const self = this;
      // console.log(this.search_val);
      AMap.plugin("AMap.Autocomplete", function () {
        // 实例化Autocomplete
        var autoOptions = {
          //city 限定城市，默认全国
          city: self.city,
        };
        var autoComplete = new AMap.Autocomplete(autoOptions);
        autoComplete.search(self.search_val, function (status, result) {
          // 搜索成功时，result即是对应的匹配数据
          // console.log(result);
          self.areaListAdd = result.tips;
        });
      });
    },
    selectAddress(item, index) {
      if (index === 1) {
        this.$store.dispatch("setAddress", item.region + item.detailedAddress);
        this.$router.push("/");
      } else {
        this.$store.dispatch(
          "setAddress",
          item.district + item.address + item.name
        );
        this.$router.push("/");
      }
    },
  },
};
</script>
<style scoped lang="scss">
.header {
  width: 100%;
  height: 18vh;
  color: #333;
  .til {
    width: 100%;
    height: 10vh;
    display: flex;
    // justify-content: space-between;
    align-items: center;
    font-size: 18px;
    span {
      width: 56%;

      display: flex;
      justify-content: right;
    }
  }
  .search {
    width: 100%;
    height: 6vh;
    background: #eee;
    display: flex;
    align-items: center;
    border-radius: 20px;
    .city {
      padding: 0 10px;
      // background: red;
      margin-left: 10px;
    }
    input {
      margin-left: 5px;
      background: #eee;
      outline: none;
      border: none;
    }
  }
}
.main {
  width: 100%;
  height: 80vh;
  background: #eee;
  overflow: hidden;
  .van-button {
    border-radius: 20px;
    bottom: 10px;
    position: fixed;
  }
  .address-list {
    width: 100%;
    height: auto;
    .addressa {
      width: 95vw;
      height: 10vh;
      background: #fff;
      margin: 0 auto;
      border-radius: 10px;
      margin-top: 10px;
      display: flex;
      align-items: center;
      color: #333;
      .addre {
        width: 100%;
        margin-left: 5vw;
        .til {
          font-size: 16px;
          color: #333;
          p {
            span {
              margin-left: 2vw;
            }
          }
        }
      }
      .edit {
        margin-right: 3vw;
        a {
          color: #333;
        }
      }
      .detailedAddres {
        font-size: 12px;
        color: #333;
      }
    }
  }
  .tips {
    width: 100%;
    height: 8vh;
    background: #fff;
    border-top: 1px solid #ccc;
    border-bottom: 1px solid #ccc;
    margin-bottom: -1px;
    display: flex;
    align-items: center;
    color: #333;
    dt {
      height: 3.5vh;
    }
    dd {
      color: rgb(129, 129, 129);
      width: 75%;

      font-size: 14px;
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
    }
  }
  .address {
    width: 100%;
    height: 10vh;
    background: #fff;
    border-radius: 10px;
    margin-top: 10px;
    display: flex;
    align-items: center;
    color: #333;
    i {
      font-size: 20px;
      margin: 0 10px;
    }
    .el-icon-aim {
      color: rgb(255, 115, 0);
      margin: 0;
    }
    p {
      color: rgb(255, 115, 0);
    }
    span {
      width: 60%;
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
    }
  }
}
</style>
